$(function(){
    //获取id
    var id = tools.queryString().id || 1;
    var token = localStorage.token;
    console.log(token);
    //获取推文数据
    $.ajax({
        type: "get",
        url: domain.test + "/gettweet",
        // url:domain.online + "/api/content",
        data:{id:id},
        beforeSend: function(xhr) {
            xhr.setRequestHeader("token",token);
        },
        success:function(data) {
            if(data.data.rtnCode === 200) {
            // if(data.rtnCode === 200) {
                var tweetDetail = data.data.data;
                console.log(tweetDetail);
                // var tweetDetail = data.data;
                document.title = tweetDetail.title;
                $(".contentBox").html(template("contentBox",{tweetDetail:tweetDetail}));
                $(".recommendProduct").html(template("recommendProduct",{plist:tweetDetail.plist}));
                var click = {}
                click.thumb = tweetDetail.thumb;
                click.status = tweetDetail.loving;
                $(".click").html(template("click",{click:click}));
            }
        }
    });
    //推文点赞
    $(".click").on("click",".clickImg",function() {
        var status = parseInt($(this).attr("data-status"));
        if(status===1) return false;
        if(status===0) {
            if(!token) {
                location.href = "../loginAndReg/login.html";
            }
            $.ajax({
                type: "get",
                url: domain.test + "/thumbtweet",
                data:{id:id},
                beforeSend: function (xhr) {
                    xhr.setRequestHeader("token",token);
                },
                success: function (response) {
                    console.log(response);
                    if(response.data.rtnCode===200) {
                        $(".clickNum span").html(parseInt($(".clickNum span").html())+1);
                        $(".clickImg img").attr("src","../../static/img/zan_S@3x.png");
                        $(".clickImg").attr("data-status",1);
                    }
                }
            });
        }
    });
    //查看详情
    $(".recommendProduct").on("click",".productItem",function(){
        var pID = $(this).attr("data-id");
        location.href = "../classify/product.html?id=" + pID;
    });
});